package com.xudow.app.ui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import com.activeshare.edu.ucenter.common.messages.sys.SchoolMessage;
import com.activeshare.edu.ucenter.common.messages.sys.StudentGradeMessage;
import com.activeshare.edu.ucenter.common.messages.user.StudentForParentMessage;
import com.activeshare.edu.ucenter.models.base.SysStudentGrade;
import com.activeshare.edu.ucenter.models.sys.school.SchoolWithOther;
import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取公办学生列表Task
 *
 * @author lukecheng
 */
public class SysSchoolsTask extends AsyncTask<Void, Void, Void> {
    private static final String TAG = makeLogTag(SysSchoolsTask.class);

    private static final String SYS_SCHOOLS_URL = Config.BASE_URL + Config.SYS_SCHOOLS_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public SysSchoolsTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(List<SchoolWithOther> schools) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        SchoolWithOther[] schoolArr = new SchoolWithOther[schools.size()];
        for (int i = 0; i < schools.size(); i++) {
            schoolArr[i] = schools.get(i);
        }

        Bundle data = new Bundle();
        data.putSerializable("schools", schoolArr);
        message.setData(data);

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(Void... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        RestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        try {
            SchoolMessage result = restTemplate.getForObject(SYS_SCHOOLS_URL,
                    SchoolMessage.class);

            if (result != null) {
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result.getSchools());
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(e.getMessage());
        }

        return null;
    }
}
